import { sortBy } from 'underscore'

export const getuser = state => state.user
export const getschool = state => state.school
export const getschooladdress = state => {
  const id = state.user ? state.user.schooladdressid : null
  if (id && state.schooladdress) {
    return state.schooladdress.filter(v => v.ID == id)
  }
  return state.schooladdress
}
export const getschooluser = state => {
  const id = state.user ? state.user.schooladdressid : null
  if (id && state.schooluser) {
    return state.schooluser.filter(v => {
      if (!v.schooladdressid) return true;
      if (v.schooladdressid == id) return true;
      if (v.isteacher) {
        if (v.classscope) {
          if (v.classscope == '*' || v.classscope.split(',').includes(id)) return true
        }
      }
    })
  }
  return state.schooluser
}
export const getschoolclassroom = state => {
  const id = state.user ? state.user.schooladdressid : null
  if (id && state.schoolclassroom) {
    state.schoolclassroom.filter(v => v.schooladdressid == id)
  }
  return state.schoolclassroom
}
export const getschoolrole = state => state.schoolrole
export const getschoolauthtype = state => state.schoolauthtype
export const getschoolcourse = state => state.schoolcourse
export const getschoolclasshour = state => {
  const data = state.schoolclasshour || []
  return sortBy(data, v => v.ID)
}
export const getschoolclazz = state => {
  const id = state.user ? state.user.schooladdressid : null
  if (id && state.schoolclazz) {
    return state.schoolclazz.filter(v => v.schooladdressid == id)
  }
  return state.schoolclazz
}
export const getschoolholiday = state => state.schoolholiday
export const getschoolclasstime = state => state.schoolclasstime
export const getpackegekey = state => state.packegekey
export const getschoolstudent = state => {
  const id = state.user ? state.user.schooladdressid : null
  if (id && state.schoolstudent) {
    return state.schoolstudent.filter(v => v.schooladdressid == id)
  }
  return state.schoolstudent

}
export const getschoolhomework = state => {
  const id = state.user ? state.user.ID : null
  if (id && state.schoolhomework) {
    return state.schoolhomework.filter(v => v.uid == id)
  }
  return state.schoolhomework
}
export const getschoolannouncementpush = state => {
  const id = state.user ? state.user.ID : null
  if (id && state.schoolannouncementpush) {
    return state.schoolannouncementpush.filter(v => v.uid == id)
  }
  return state.schoolannouncementpush
}
export const getschoolitembankgroup = state => state.schoolitembankgroup


export const getschoolbyid = state => ID => state.school.find(v => v.ID === ID) || { name: '没有权限' }
export const getschooladdressbyid = state => ID => state.schooladdress.find(v => v.ID === ID) || { name: '没有权限' }
export const getschooluserbyid = state => ID => state.schooluser.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolclassroombyid = state => ID => state.schoolclassroom.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolrolebyid = state => ID => state.schoolrole.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolauthtypebyid = state => ID => state.schoolauthtype.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolcoursebyid = state => ID => state.schoolcourse.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolclasshourbyid = state => ID => state.schoolclasshour.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolclazzbyid = state => ID => state.schoolclazz.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolholidaybyid = state => ID => state.schoolholiday.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolclasstimebyid = state => ID => state.schoolclasstime.find(v => v.ID === ID) || { name: '没有权限' }
export const getpackegekeybyid = state => ID => state.packegekey.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolstudentbyid = state => ID => state.schoolstudent.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolhomeworkbyid = state => ID => state.schoolhomework.find(v => v.ID === ID) || { name: '没有权限' }
export const getschoolannouncementpushbyid = state => ID => state.schoolannouncementpush.find(v => v.ID === ID) || { name: '没有权限' }

export const getschoolitembankgroupbyid = state => ID => state.schoolitembankgroup.find(v => v.ID === ID) || { name: '没有权限' }

export const getstate = state => state.state
